home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / debugger / bdm-linu.0 / bdm-linu / bdm-linux / bdm-icd.c next >
Encoding:
C/C++ Source or Header  |  1995-04-27  |  4.2 KB  |  156 lines

  1. /* bdm-icd.c - routines to talk to CPU16 or CPU32 target
  2.  * via ICD interface hardware on IBM PC parallel printer port
  3.  * Copyright (C) 1992 by Scott Howard, all rights reserved
  4.  * Permission is hereby granted to freely copy and use this code or derivations thereof
  5.  * as long as no charge is made to anyone for its use
  6.  *
  7.  * Ported to Linux by D.Jeff Dionne, April 25, 1995
  8.  *
  9.  * NOTE: ICD is a product of P&E Microsystems Inc.
  10.  */
  11.  
  12. #include    "bdm.h"
  13. #include    "bdmcalls.h"
  14. #include    "bdmerror.h"
  15. #include    "trgtstat.h"
  16.  
  17. #define    bdm_ctl         0               /* offset of control port from base    */
  18. #define    dsi             1               /* data shift input - PC->MCU          */
  19. #define    dsclk           2               /* data shift clock/breakpoint pin     */
  20. #define    step_out        4               /* set low to force breakpoint           */
  21. #define    rst_out         8               /* set low to force reset on MCU       */
  22. #define    oe        0x10        /* set to a 1 to enable DSI            */
  23. #define    force_berr    0x40        /* set to a 1 to force BERR on target  */
  24.  
  25. #define    bdm_stat        1               /* offset of status port from base     */
  26. #define    freeze        0x40        /* FREEZE asserted when MCU stopped    */
  27. #define    dso             0x80            /* data shift output - MCU->PC         */
  28.  
  29. #define    waitcnt         0xffff          /* no of loops to wait for response    */
  30.  
  31. extern void bdm_delay (unsigned);
  32. extern void bdm_error (int);
  33.  
  34. static unsigned bdm_speed, bdm_port;
  35. extern unsigned go_cmd, CommandBitCount;
  36. unsigned StatMask = TARGETSTOPPED;
  37.  
  38. void bdm_init (int port, int baud)
  39. {
  40.     RegsValid = 0;
  41.     bdm_port = port;
  42.     bdm_speed = baud;
  43.     outportb (bdm_port+bdm_ctl,step_out | dsclk | rst_out);
  44. }
  45.  
  46. void bdm_deinit (void)
  47. {}
  48.  
  49. /* StopChip stops MCU in preparation for sending command     */
  50.  
  51. int StopChip (void)
  52. {
  53.     unsigned ctr;
  54.     char frozen = 0;
  55.  
  56.     RegsValid = 0;
  57.     if (inportb (bdm_port + bdm_stat) & freeze) return frozen;
  58.     frozen = 1;
  59.     outportb (bdm_port + bdm_ctl, dsclk + rst_out);
  60.     for (ctr = waitcnt; ctr; ctr--)
  61.     {
  62.         if (inportb (bdm_port + bdm_stat) & freeze) break;
  63.         bdm_delay (1);
  64.     }
  65.     if (!ctr)
  66.     {
  67.         outportb (bdm_port + bdm_ctl, dsclk | rst_out | force_berr);
  68.         bdm_delay (waitcnt);
  69.         for (ctr = waitcnt; ctr; ctr--)
  70.         {
  71.             if (inportb (bdm_port + bdm_stat) & freeze) break;
  72.             bdm_delay (1);
  73.         }
  74.     }
  75.     outportb (bdm_port + bdm_ctl, dsclk | rst_out | step_out);
  76.     if (!ctr) bdm_error (BDM_FAULT_RESPONSE);
  77.     return frozen;
  78. }
  79.  
  80. /* RestartChip resets target MCU, then stops it on first instruction fetch */
  81.  
  82. void RestartChip (void)
  83. {
  84.     unsigned LoopCount;
  85.  
  86.     outportb (bdm_port + bdm_ctl, dsclk);
  87.     bdm_delay (waitcnt);
  88.     StopChip ();
  89. }
  90.  
  91. /* ResetChip applies hardware reset to the target MCU */
  92.  
  93. void ResetChip (void)
  94. {
  95.     RegsValid = 0;
  96.     outportb (bdm_port + bdm_ctl, dsclk | step_out);
  97.     bdm_delay (waitcnt);
  98.     outportb (bdm_port + bdm_ctl, dsclk | rst_out | step_out);
  99. }
  100.  
  101. /* bdm_clk sends <value> to MCU for <parameter> bits, returns MCU response */
  102.  
  103. LONG bdm_clk (WORD value, int count)
  104. {
  105.     LONG ShiftRegister = ((LONG) value) << (32 - count);
  106.  
  107.     unsigned char DataOut;
  108.  
  109.     while (count--)
  110.     {
  111.         DataOut = (ShiftRegister & 0x80000000) ? dsi : 0;
  112.         ShiftRegister <<= 1;
  113.         if (!(inportb (bdm_port + bdm_stat) & dso))
  114.             ShiftRegister |= 1;
  115.         outportb (bdm_port + bdm_ctl, DataOut | rst_out | oe | step_out);
  116.         bdm_delay (bdm_speed + 1);
  117.         outportb (bdm_port + bdm_ctl, DataOut | rst_out | oe | step_out | dsclk);
  118.         bdm_delay ((bdm_speed >> 1) + 1);
  119.     }
  120.  
  121.     outportb (bdm_port + bdm_ctl, dsclk | step_out | rst_out);
  122.     return ShiftRegister;
  123. }
  124.  
  125. /* StepChip sends GO command word, then triggers breakpoint on first fetch        */
  126.  
  127. void StepChip (void)
  128. {
  129. #define    DataOut    (go_cmd & 1 ? dsi : 0)
  130.  
  131.     bdm_clk (go_cmd >> 1, CommandBitCount - 1);
  132.     outportb (bdm_port + bdm_ctl, oe | step_out | DataOut | rst_out);
  133.     bdm_delay (bdm_speed + 1);
  134.     disable ();
  135.     outportb (bdm_port + bdm_ctl, oe | DataOut | dsclk | rst_out);
  136.     outportb (bdm_port + bdm_ctl, dsclk | rst_out);
  137.     enable ();
  138.     StopChip ();
  139. }
  140.  
  141. /* GetStatus is called by user code to get current status of CPU signals */
  142.  
  143. unsigned GetStatus (void)
  144. {
  145.     BYTE temp = inportb (bdm_port + bdm_stat);
  146.  
  147.     return     (temp & freeze ? TARGETSTOPPED: 0);
  148. }
  149.  
  150. /* GetStatusMask returns mask showing which stat bits are valid */
  151.  
  152. unsigned GetStatusMask (void)
  153. {
  154.     return TARGETSTOPPED;
  155. }
  156.